package com.geostar.constructionland.analyse.entity.alyresult.json;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.geostar.constructionland.analyse.entity.alyresult.AnalysisResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author yelll
 * @since 2020-6-1 20:47
 *
 * ********************重要提示*******************
 * 在deserialize方法中的JsonParser,无法使用getText()获取json值
 * 需要使用readValueAsTree来读取数据
 **/
@Component
@Slf4j
public class AnalysisResultDeserializer extends JsonDeserializer<AnalysisResult> {
    public AnalysisResultDeserializer(){
        super();
    }
    @Override
    public AnalysisResult deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        log.info("AnalysisResult");
        //得到 codec， 通过调试可以知道，ta是一个 object mapper 对象
        ObjectCodec codec = p.getCodec();
        //读出所有的字段，得到 TreeNode 对象
        TreeNode readValueAsTree = p.readValueAsTree();
        //得到某个属性 的 node
        TreeNode treeNode = readValueAsTree.get("ResultRecords");
        AnalysisResult analysisResult = new AnalysisResult();
        analysisResult.setResultRecords(treeNode.toString());
        return analysisResult;
    }
}
